<?php


namespace app\order\admin;


use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\user\model\Role as RoleModel;
use think\Db;
use app\loan\model\Reference as ReferenceModel;

class Reference extends Admin
{
    protected  $tableName  = 'reference';
    public function index(){
        $map        = $this->getMap();
        $data = Db::name($this->tableName)->where($map)->paginate();
        return ZBuilder::make('table')
            ->setTableName($this->tableName)
            ->hideCheckbox()
            ->addColumns([
                ['phone','手机号'],
                ['name','姓名'],
                ['number','工号'],
                ['create_time','创建时间'],
                ['invite_num','已邀请人数'],
                ['right_button','操作'],
            ])
            ->addRightButton('edit')
            ->addRightButton('custom',[
                'title' => '删除',
                'icon'  => 'fa fa-times',
                'class' => 'btn btn-xs btn-default ajax-get confirm',
                'href'  => url('delete',['id'=>'__id__'])
            ])
            ->addTopButton('add')
            ->setPages($data->render())
            ->setRowList($data)
            ->fetch();
    }


    public function add(){

        if($this->request->isPost()){
            $param = $this->request->param();
            ReferenceModel::create($param);
            $data  = [
                'username'  => $param['phone'],
                'nickname'  => $param['name'],
                'role'      => '2',
                'email'     => '',
                'password'  => $param['password'],
                'mobile'    => $param['phone'],
                'status'    => 1,
            ];
            if (session('user_auth.role') != 1) {
                if ($data['role'] == session('user_auth.role')) {
                    $this->error('禁止创建与当前角色同级的用户');
                }
                $role_list = RoleModel::getChildsId(session('user_auth.role'));
                if (!in_array($data['role'], $role_list)) {
                    $this->error('权限不足，禁止创建非法角色的用户');
                }
            }
            if(ReferenceModel::createBackendAccount($data)){
                $this->success('创建成功',url('index'));
            }

            $this->error('创建失败');
        }
        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'phone', '手机号'],
                ['text', 'name', '姓名'],
                ['text', 'number', '工号'],
                ['password','password','密码'],
                ['hidden','create_time',date('Y-m-d H:i:s')]
            ])
            ->fetch();
    }

    public function edit($id=''){

        if($this->request->isPost()){
            $param          =   $this->request->only(['phone','number','name','password']);
            if(strlen($param['password']) < 8){
                $this->error('密码至少8位');
            }
            ReferenceModel::update($param,['id'=>$id]);
            ReferenceModel::updateBackendAccount($param);
            $this->success('修改成功',url('index'));
        }

        $data   =   ReferenceModel::findOrEmpty($id);
        return ZBuilder::make('form')
            ->addFormItems([
                ['text', 'phone', '手机号','',$data['phone']],
                ['text', 'name', '姓名','',$data['name']],
                ['text', 'number', '工号','',$data['number']],
                ['password','password','密码','',$data['password']],
                ['hidden','id','',$id]
            ])
            ->fetch();

    }

    public function delete($id='')
    {
        if(ReferenceModel::deleteAccount($id)){
            $this->success('成功');
        }
    }

}